/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package trabajo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;

/**
 *
 * @author Jean carlos
 */
public class aplicacion {

    static File arch = new File("E:\\unico2.txt");

    public static void main(String[] args) throws Exception {
        ArrayList<alumno> lista = new ArrayList<alumno>();
        Inicio(lista);
        Crear_Alumno(lista);
        Mostrar(lista);
        Fin(lista);
    }

    public static void Inicio(ArrayList<alumno> lista) throws Exception {
        if (!arch.exists()) {
            arch.createNewFile();
        }
        FileReader fr = new FileReader(arch);
        BufferedReader br = new BufferedReader(fr);
        String tem;
        while (true) {
            try {
                tem = br.readLine();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "No existen datos" + e.getMessage());
                return;
            }
            if (tem == null) {
                break;
            }
            String v[] = tem.split(",");
            lista.add(new alumno(v[0], v[1], v[2]));
        }

    }

    public static void Crear_Alumno(ArrayList<alumno> lista) {
        Scanner sc = new Scanner(System.in);
        String name, id, facu;
        do {
            System.out.println("Ingrese Nombre");
            name = sc.next();
            if (name.length() > 2) {
                break;
            }
        } while (true);

        do {
            boolean band = false;
            System.out.println("Ingrese DNI");
            id = sc.next();
            for (int i = 0; i < lista.size(); i++) {
                if (lista.get(i).getDni().equalsIgnoreCase(id)) {
                    band = true;
                }
            }
            if (band == false) {
                break;
            }
        } while (true);

        do {
            System.out.println("Ingrese Facultad");
            facu = sc.next();
            if (facu.length() > 5) {
                break;
            }
        } while (true);
        lista.add(new alumno(name, id, facu));
    }

    public static void Fin(ArrayList<alumno> lista) throws Exception {
        if (!arch.exists()) {
            arch.createNewFile();
        }
        FileWriter fw = new FileWriter(arch);
        PrintWriter pw = new PrintWriter(fw);
        String txt;
        for (int i = 0; i < lista.size(); i++) {
            txt = lista.get(i).name + "," + lista.get(i).dni + "," + lista.get(i).facul;
            pw.println(txt);
        }
        pw.close();
        fw.close();
    }

    public static void Mostrar(ArrayList<alumno> lista) {
        for (int i = 0; i < lista.size(); i++) {
            System.out.println(lista.get(i).name + "," + lista.get(i).dni + "," + lista.get(i).facul);

        }
    }

}
